package com.whz.governance.redislimiter.filter;

import com.beust.jcommander.internal.Lists;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author whz
 */
public class CompositeRedisRateLimitSupport implements RateLimitSupporter {

    private final List<RedisRateLimitSupporter> supporterList;

    public CompositeRedisRateLimitSupport(final List<RedisRateLimitSupporter> supporterList) {
        Assert.notEmpty(supporterList, "supporterList REQUIRED.");
        this.supporterList = Lists.newLinkedList(supporterList);
    }

    @Override
    public boolean match(String requestUri) {
        return supporterList.stream().anyMatch(s -> s.match(requestUri));
    }

    @Override
    public boolean allowAccess(HttpServletRequest request) {
        return supporterList.stream()
                            .filter(s -> s.match(request.getRequestURI()))
                            .allMatch(s -> s.allowAccess(request));
    }
}